package com.passby;

import view.LoginView;
import view.MainView;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.PassByCache;

public class PassByActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	final PassByCache cache = new PassByCache(this);
	    final String user = cache.getUser();
	    final String accessToken = cache.getAccessToken();
	    final String tokenSecret = cache.getTokenSecret();
	    
        if(user!=null&&accessToken!=null&&tokenSecret!=null)
        {
        	new LoginTask() { 
		        protected void onPostExecute(Boolean result) {
		            if (result) {
		            	cache.setUser(Client.getInstance().self, Client.accessor.accessToken, Client.accessor.tokenSecret);
		            	Intent intent = new Intent();   
				        intent.setClass(PassByActivity.this, MainView.class);   
				        startActivity(intent);  
				        finish();
		            }else
		            {
		            	Intent intent = new Intent();   
		                intent.setClass(PassByActivity.this, LoginView.class);   
		                startActivity(intent);   
		                finish();
		            }
		        }
		    }.execute(accessToken,tokenSecret);
        }else
        {
        	Intent intent = new Intent();   
            intent.setClass(PassByActivity.this, LoginView.class);   
            startActivity(intent);   
            finish();
        }
    }
    
    private class LoginTask extends AsyncTask<String, Integer, Boolean> {
	    @Override
	    protected Boolean doInBackground(String... arg0) {
	    	Boolean b = false;
			b = Client.getInstance().loginWithToken(arg0[0], arg0[1]);
	        return b;
	    }
	}
}